define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/unit/index' + location.search,
                    add_url: 'basic/unit/add',
                    edit_url: 'basic/unit/edit',
                    del_url: 'basic/unit/del',
                    multi_url: 'basic/unit/multi',
                    import_url: 'basic/unit/import',
                    table: 'unit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: false,
                searchFormVisible: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'category_id', title: __('Category_id'),
                            searchList: $.getJSON("basic/unit/getCategory")
                        },
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'initial_receivables', title: __('Initial_receivables'), operate: false},
                        {field: 'accrued_receivable', title: __('Accrued_receivable'), operate: false},
                        {field: 'initial_payables', title: __('Initial_payables'), operate: false},
                        {field: 'accrued_payables', title: __('Accrued_payables'), operate: false},
                        {
                            field: 'spell',
                            title: __('Spell'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'contacts',
                            title: __('Contacts'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'phone', title: __('Phone'), operate: false},
                        {
                            field: 'address',
                            title: __('Address'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'postcode',
                            title: __('Postcode'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'fax',
                            title: __('Fax'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'email',
                            title: __('Email'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        //     {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        profit: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/unit/profit' + location.search,
                    add_url: 'basic/unit/add',
                    // edit_url: 'basic/unit/edit',
                    // del_url: 'basic/unit/del',
                    multi_url: 'basic/unit/multi',
                    import_url: 'basic/unit/import',
                    table: 'unit',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                var data = data.data;
                $("#expenditure").text(data.expenditure);
                $("#income").text(data.income);
                $("#sum_cost_amount").text(data.sum_cost_amount);
                $("#sum_other_amount").text(data.sum_other_amount);
                $("#sum_other_zc_amount").text(data.sum_other_zc_amount);
                $("#sum_project_amount").text(data.sum_project_amount);
                $("#sum_project_zc_amount").text(data.sum_project_zc_amount);
                $("#sum_sx_amount").text(data.sum_sx_amount);
            });
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                //$("input[name='unit_id']", form).addClass("selectpage").data("source", "basic/unit/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                // $("input[name='stash_id']", form).addClass("selectpage").data("source", "basic/stash/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                $("input[name='staff_id']", form).addClass("selectpage").data("source", "basic/staff/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                // $("input[name='assets_id']", form).addClass("selectpage").data("source", "basic/assets/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: false,
                searchFormVisible: true,
                fixedRightNumber: 1,
                pageSize: 200,
                pageList: [200, 400, 1000, 'All'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        // {field: 'category_id', title: __('Category_id'),
                        //     searchList: $.getJSON("basic/unit/getCategory")},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'staff_id', title: __('经手人'), visible: false,},
                        {field: 'sum_amount', title: __('销售收入'), operate: false},
                        {field: 'project_sr_amount', title: __('项目收入'), operate: false},
                        {field: 'other_sr_amount', title: __('其他收入'), operate: false},
                        {field: 'sum_cost_amount', title: __('销售成本'), operate: false},
                        {field: 'project_zc_amount', title: __('项目支出'), operate: false},
                        {field: 'other_zc_amount', title: __('其他支出'), operate: false},
                        {
                            field: 'date',
                            title: __('时间查询'),
                            align: 'center',
                            formatter: Table.api.formatter.datetime,
                            defaultValue: this.getFirstDayOfMonth() + " 00:00:00 - " + this.getLastDayOfMonth() + ' 23:59:59',
                            operate: 'RANGE',
                            visible: false,
                            addclass: 'datetimerange'
                        }
                        //     {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate
                        // }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        refund: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/unit/refund' + location.search,
                    add_url: 'basic/unit/add',
                    // edit_url: 'basic/unit/edit',
                    // del_url: 'basic/unit/del',
                    multi_url: 'basic/unit/multi',
                    import_url: 'basic/unit/import',
                    table: 'unit',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                var data = data.data;
                // $("#expenditure").text(data.expenditure);
                // $("#income").text(data.income);
                // $("#sum_cost_amount").text(data.sum_cost_amount);
                // $("#sum_other_amount").text(data.sum_other_amount);
                // $("#sum_other_zc_amount").text(data.sum_other_zc_amount);
                // $("#sum_project_amount").text(data.sum_project_amount);
                // $("#sum_project_zc_amount").text(data.sum_project_zc_amount);
                // $("#sum_sx_amount").text(data.sum_sx_amount);
            });
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                //$("input[name='unit_id']", form).addClass("selectpage").data("source", "basic/unit/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                // $("input[name='stash_id']", form).addClass("selectpage").data("source", "basic/stash/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                $("input[name='staff_id']", form).addClass("selectpage").data("source", "basic/staff/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                // $("input[name='assets_id']", form).addClass("selectpage").data("source", "basic/assets/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: false,
                searchFormVisible: true,
                fixedRightNumber: 1,
                pageSize: 200,
                pageList: [200, 400, 1000, 'All'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        // {field: 'category_id', title: __('Category_id'),
                        //     searchList: $.getJSON("basic/unit/getCategory")},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'staff_id', title: __('经手人'), visible: false,},
                        {field: 'sum_amount', title: __('销售收入'), operate: false},
                        {field: 'project_sr_amount', title: __('项目收入'), operate: false},
                        {field: 'other_sr_amount', title: __('其他收入'), operate: false},
                        {field: 'sum_cost_amount', title: __('销售成本'), operate: false},
                        {field: 'project_zc_amount', title: __('项目支出'), operate: false},
                        {field: 'other_zc_amount', title: __('其他支出'), operate: false},
                        {
                            field: 'date',
                            title: __('时间查询'),
                            align: 'center',
                            formatter: Table.api.formatter.datetime,
                            defaultValue: this.getFirstDayOfMonth() + " 00:00:00 - " + this.getLastDayOfMonth() + ' 23:59:59',
                            operate: 'RANGE',
                            visible: false,
                            addclass: 'datetimerange'
                        }
                        //     {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate
                        // }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'basic/unit/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'basic/unit/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'basic/unit/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        getFirstDayOfMonth: function getFirstDayOfMonth() {
            const currentDate = new Date();
            const firstDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
            return this.formatDate(firstDay)
        },
        getLastDayOfMonth: function getLastDayOfMonth() {
            const currentDate = new Date();
            const lastDay = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
            return this.formatDate(lastDay);
        },
        formatDate: function formatDate(date) {
            const year = date.getFullYear();
            const month = ("0" + (date.getMonth() + 1)).slice(-2);
            const day = ("0" + date.getDate()).slice(-2);
            return year + "-" + month + "-" + day;
        }
    };
    return Controller;
});
